package 算法.练习1;

import java.math.BigDecimal;

/**
 * @author:谢君臣
 * @Date:2021/8/16-15:41
 * @version:1.0
 * @function:
 */
public class NC128 {
    public static void main(String[] args) {
        maxWater(new int[]{0,1,0,2,1,0,1,3,2,1,2,1});
    }
    public static long maxWater (int[] arr) {
        // write code here
        // write code here
        //定义一个用来累加的变量
        long count = 0L;
        //定义起始的左边界
        int from = 0;
        //定义一个起始的右边界
        int to = arr.length - 1;
        while(from < to){
            //找到最小的边界
            int min = Math.min(arr[from], arr[to]);
            //如果左边界最小则从左向右移动，并且计算容量
            while(from < to && arr[from] <= min){
                count += min - arr[from];
                from ++;
            }
            //如果右边界最小则从右向左移动，并且计算容量
            while(from < to && arr[to] <= min){
                count += min - arr[to];
                to --;
            }
        }
        return count;
    }
}
